<?php

class Calendar
{

	private function __construct()
	{
		
	}
	
	public static function getPlannedTeamGames(Team $oTeam)
	{
	
		$query = "SELECT Game.*
						FROM Game						
						WHERE Game.date >= CURDATE()
						AND
						(
							Game.team1_nefub_id = '".$oTeam->nefub_id."'
							OR
							Game.team2_nefub_id = '".$oTeam->nefub_id."'														
							OR
							Game.nefub_id IN
							(
								SELECT Referee.game_nefub_id
								FROM Referee
								WHERE
								Referee.team_nefub_id = '".$oTeam->nefub_id."'
							)
						)
						AND Game.date IS NOT NULL													
					ORDER BY Game.date ASC,
					Game.time ASC";
		return Model::getAllFromQuery('Game', $query);
	}
	
	public static function getPlannedPersonGames(Person $oPerson = null)
	{
		$teamPersonSelect = "
							SELECT TeamPerson.team_nefub_id
						 	FROM TeamPerson
						 	WHERE TeamPerson.person_nefub_id = '".$oPerson->nefub_id."'";
		
		$query = "SELECT Game.*
						FROM Game						
						WHERE Game.date >= CURDATE()
						AND
						(
							Game.team1_nefub_id IN
							(
								$teamPersonSelect
							)
							OR
							Game.team2_nefub_id IN
							(
								$teamPersonSelect
							)
							
							OR
							Game.nefub_id IN
							(
								SELECT Referee.game_nefub_id
								FROM Referee
								WHERE
								Referee.person_nefub_id = '".$oPerson->nefub_id."'
								OR
								Referee.team_nefub_id IN
								(
									$teamPersonSelect
								)
							)
						)
						AND Game.date IS NOT NULL													
					ORDER BY Game.date ASC,
					Game.time ASC";
		return Model::getAllFromQuery('Game', $query);
	}	
	
	/**
	 * @deprecated
	 */
	public static function getGenderGames(Season $oSeason, Gender $oGender)
	{
		$query = "SELECT Game.*
					FROM Game 		
					LEFT JOIN Competition ON Game.competition_nefub_id = Competition.nefub_id			
					WHERE Game.date >= CURDATE()
					
					AND Game.date IS NOT NULL					
					AND Competition.season_nefub_id = '".$oSeason->nefub_id."'					
					AND Competition.gender_id = '".$oGender->getId()."'				
				ORDER BY Game.date ASC,
					Game.time ASC
					";
		return self::sortByDateAndLocation($query);
	}
	
	public static function getPlannedGamesSortedByDateAndLocation(Season $oSeason)
	{
		$query = "SELECT Game.*
					FROM Game 		
					LEFT JOIN Competition ON Game.competition_nefub_id = Competition.nefub_id			
					WHERE Game.date >= CURDATE()
					
					AND Game.date IS NOT NULL					
					AND Competition.season_nefub_id = '".$oSeason->nefub_id."'								
				ORDER BY Game.date ASC,
					Game.time ASC
					";
		return self::sortByDateAndLocation($query);
	}
	
	/**
	 * @deprecated
	 */
	public static function getPlannedGames(Season $oSeason, Gender $oGender = null, $date = null)
	{
		if($oGender)
		{
			return self::getGenderGames($oSeason, $oGender);
		}
		
		if($date)
		{
			$dateWhere = "Game.date = '".str_replace("'", '', $date) ."'";
		}
		else
		{
			$dateWhere = 'DATE(Game.date) >= CURDATE()';
		}
		
		$query = "SELECT Game.*
					FROM Game 		
					LEFT JOIN Competition ON Game.competition_nefub_id = Competition.nefub_id			
					WHERE 
						".$dateWhere."
					
					AND Game.date IS NOT NULL					
					AND Competition.season_nefub_id = '".$oSeason->nefub_id."'								
				ORDER BY Game.date ASC,
					Game.time ASC
					";
		return Model::getAllFromQuery('Game', $query);
	}
	
	protected static function sortByDateAndLocation($query)
	{
		$dates = array();
		$locations = array();
		
		$result = Database::query($query);

		$rows = Database::convertResult($result);
		foreach ($rows as $row)
		{
			$oGame = new Game();			
			$oGame->setData($row);
			
			if(!($locations[$oGame->location_nefub_id]))
			{
				$locations[$oGame->location_nefub_id] = Location::getByNefubId($oGame->location_nefub_id);
			}
			$oLocation = $locations[$oGame->location_nefub_id];
			
			
			// create array for locations per date
			if(!is_array($dates[$oGame->date]))
			{
				$dates[$oGame->date] = array();
			}
			
			// create array for times per location
			if(!is_array($dates[$oGame->date][$oGame->location_nefub_id]))
			{
				$dates[$oGame->date][$oGame->location_nefub_id] = array(
				'location' => $oLocation,
				'gameTimes' => array());
			}
			
			// create array for times per location
			if(!is_array($dates[$oGame->date][$oGame->location_nefub_id]['gameTimes'][$oGame->time]))
			{
				$dates[$oGame->date][$oGame->location_nefub_id]['gameTimes'][$oGame->time] = array();
			}
			
			$dates[$oGame->date][$oGame->location_nefub_id]['gameTimes'][$oGame->time][] = $oGame;
		}

		return $dates;
	}
	
	public static function getPlannedLocationGames(Location $oLocation)
	{
		$where[] = "Game.location_nefub_id = '".$oLocation->nefub_id."'";
		$where[] = "Game.date >= CURDATE()";
		$where[] = "Game.date IS NOT NULL";					
		
		$where = implode(' AND ', $where);
		
		$query = "SELECT Game.*
					FROM Game 					
					WHERE $where					
					ORDER BY Game.date ASC,
					Game.time ASC
					";

		$r = Database::query($query);
		$rows = Database::convertResult($r);
		
		$dates = array();

		foreach ($rows as $row)
		{
			$oGame = new Game();			
			$oGame->setData($row);
			
			// create array for locations per date
			if(!is_array($dates[$oGame->date]))
			{
				$dates[$oGame->date] = array();
			}
			
			// create array for times per location
			if(!is_array($dates[$oGame->date]))
			{
				$dates[$oGame->date] = array();
			}
			
			// create array for times per location
			if(!is_array($dates[$oGame->date][$oGame->time]))
			{
				$dates[$oGame->date][$oGame->time] = array();
			}
			
			$dates[$oGame->date][$oGame->time][] = $oGame;
		}		
		return $dates;
	}
}